//
//  VisitorLoginView.m
//  iOS07-Weibo-LPK-OC
//
//  Created by 罗培克 on 6/28/15.
//  Copyright © 2015 lpk. All rights reserved.
//

#import "VisitorLoginView.h"

@interface VisitorLoginView ()
/// 背景 house
@property(weak, nonatomic)UIImageView *houseView;
/// 背景 icon
@property(weak, nonatomic)UIImageView *iconView;
/// 背景图片
@property(weak, nonatomic)UIImageView *bgImage;
/// 登录按钮
@property(weak, nonatomic)UIButton *loginButton;
/// 注册按钮
@property(weak, nonatomic)UIButton *registerButton;
/// 信息
@property(weak, nonatomic)UILabel *messageLabel;
@end

@implementation VisitorLoginView

-(instancetype)init {
    if (self = [super init]) {
        self.backgroundColor = [UIColor colorWithWhite:237.0/ 255.0 alpha:1.0];
        [self addSubviews];
    }
    return self;
}

- (void)setUpViewWithInfo:(NSString *)imageName message:(NSString *)message isHome:(BOOL)isHome {
    self.iconView.image = [UIImage imageNamed:imageName];
    self.messageLabel.text = message;
    
    if (!isHome) {
        self.houseView.hidden = YES;
        [self sendSubviewToBack:self.bgImage];
    }
}

-(void)startAnim {
    CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    anim.toValue = @(M_PI * 2);
    anim.duration = 20;
    anim.repeatCount = CGFLOAT_MAX;
    
    [self.iconView.layer addAnimation:anim forKey:nil];
}

/// 添加子控件并布局
- (void)addSubviews {
    UIImageView *houseView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"visitordiscover_feed_image_house"]];
    [self addSubview:houseView];
    self.houseView = houseView;
    
    UIImageView *iconView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"visitordiscover_feed_image_smallicon"]];
    [self addSubview:iconView];
    self.iconView = iconView;
    
    UIImageView *bgImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"visitordiscover_feed_mask_smallicon"]];
    self.bgImage = bgImage;
    [self addSubview:bgImage];
    
    UILabel *messageLabel = [[UILabel alloc]init];
    messageLabel.text = @"登录后，最新、最热微博尽在掌握，不再会与实事潮流擦肩而过";
    self.messageLabel = messageLabel;
    [self addSubview:messageLabel];
    messageLabel.numberOfLines = 2;
    messageLabel.preferredMaxLayoutWidth = 240;
    messageLabel.font = [UIFont systemFontOfSize:14];
    messageLabel.textColor = [UIColor darkGrayColor];
    messageLabel.textAlignment = NSTextAlignmentCenter;
    [messageLabel sizeToFit];
    
    UIButton *loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.loginButton = loginButton;
    [self addSubview:loginButton];
    [loginButton setTitle:@"登录" forState: UIControlStateNormal];
    [loginButton setBackgroundImage:[UIImage imageNamed:@"common_button_white_disable"] forState:UIControlStateNormal];
    [loginButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [self.loginButton addTarget:self action:@selector(loginButtonDidClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *registerButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.registerButton = registerButton;
    [self addSubview:registerButton];
    [registerButton setTitle:@"注册" forState: UIControlStateNormal];
    [registerButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [registerButton setBackgroundImage:[UIImage imageNamed:@"common_button_white_disable"] forState:UIControlStateNormal];
    [self.registerButton addTarget:self action:@selector(registerButtonDidClick) forControlEvents:UIControlEventTouchUpInside];
    
    // 布局 iconView
    self.iconView.translatesAutoresizingMaskIntoConstraints = NO;
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.iconView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.iconView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:-80]];
    
    // 布局 houseView
    self.houseView.translatesAutoresizingMaskIntoConstraints = NO;
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.houseView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.iconView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.houseView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.iconView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0]];
    
    // 布局 messageLabel
    self.messageLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.messageLabel attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.iconView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.messageLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.iconView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:8.0]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[subView(40)]" options:0 metrics:nil views:@{@"subView": self.messageLabel}]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[subView(240)]" options:0 metrics:nil views:@{@"subView": self.messageLabel}]];
    
    // 布局 bgImage
    self.bgImage.translatesAutoresizingMaskIntoConstraints = NO;
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[subView]|" options:0 metrics:nil views:@{@"subView": self.bgImage}]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[subView]-220-|" options:0 metrics:nil views:@{@"subView": self.bgImage}]];
    
    // 布局 registerButton
    self.registerButton.translatesAutoresizingMaskIntoConstraints = NO;
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.registerButton attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.messageLabel attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.registerButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.messageLabel attribute:NSLayoutAttributeBottom multiplier:1.0 constant:8.0]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[subView1(100)]" options:0 metrics:nil views:@{@"subView1": self.registerButton}]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[subView1(40)]" options:0 metrics:nil views:@{@"subView1": self.registerButton}]];
    
    // 布局 loginButton
    self.loginButton.translatesAutoresizingMaskIntoConstraints = NO;
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.loginButton attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.messageLabel attribute:NSLayoutAttributeRight multiplier:1.0 constant:0.0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.loginButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.messageLabel attribute:NSLayoutAttributeBottom multiplier:1.0 constant:8.0]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[subView1(100)]" options:0 metrics:nil views:@{@"subView1": self.loginButton}]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[subView1(40)]" options:0 metrics:nil views:@{@"subView1": self.loginButton}]];
}

- (void)loginButtonDidClick {
    if ([self.delegate respondsToSelector:@selector(loginButtonClick:)]) {
        [self.delegate loginButtonClick:self];
    }
}

- (void)registerButtonDidClick {
    if ([self.delegate respondsToSelector:@selector(registerButtonClick:)]) {
        [self.delegate registerButtonClick:self];
    }
}

@end
